﻿using Core;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Dapper;

namespace DAL
{
    public class DicDataDAL : BaseDAL
    {
        #region 学校类型表导入
        /// <summary>
        /// 学校类型表导入
        /// </summary>
        /// <returns></returns>
        public bool GetDicSchoolType()
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                string strDst = string.Empty;
                try
                {
                    strDst = wc.DownloadString(ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetDicSchoolType");
                }
                finally
                {
                    wc.Dispose();
                }
                if (!string.IsNullOrWhiteSpace(strDst))
                {
                    var ldst = JsonConvert.DeserializeObject<List<Model.DicSchoolType>>(strDst);

                    #region 数据入库
                    using (var conn = new SqlConnection(connection))
                    {
                        conn.Open();
                        conn.Execute("truncate table DicSchoolType;");
                        IDbTransaction transaction = conn.BeginTransaction();
                        try
                        {
                            ldst.ForEach(dst =>
                            {
                                conn.Execute("  insert into DicSchoolType(STypeName,StypeCode) values (@STypeName,@StypeCode)", dst, transaction);
                            });
                            transaction.Commit();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Log.errorlog(ex);
                        }
                        conn.Close();
                    }
                    #endregion
                }
            }
            return false;
        }
        #endregion

        #region 年级表导入
        /// <summary>
        /// 年级表导入
        /// </summary>
        /// <returns></returns>
        public bool GetDicGrade()
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                string strDg = string.Empty;
                try
                {
                    strDg = wc.DownloadString(ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetDicGrade");
                }
                finally
                {
                    wc.Dispose();
                }
                if (!string.IsNullOrWhiteSpace(strDg))
                {
                    var ldg = JsonConvert.DeserializeObject<List<Model.DicGrade>>(strDg);

                    #region 数据入库
                    using (var conn = new SqlConnection(connection))
                    {
                        conn.Open();
                        conn.Execute("truncate table DicGrade;");
                        IDbTransaction transaction = conn.BeginTransaction();
                        try
                        {
                            ldg.ForEach(dg =>
                            {
                                conn.Execute("insert into DicGrade(ID,StypeCode,GradeName,GradeCode) values (@ID,@StypeCode,@GradeName,@GradeCode)", dg, transaction);
                            });
                            transaction.Commit();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Log.errorlog(ex);
                        }
                        conn.Close();
                    }
                    #endregion
                }
            }
            return false;
        }
        #endregion

        #region 地区表导入
        /// <summary>
        /// 地区表导入
        /// </summary>
        /// <returns></returns>
        public bool GetDicArea()
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                string strArea = string.Empty;
                try
                {
                    strArea = wc.DownloadString(ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetDicArea");
                }
                finally
                {
                    wc.Dispose();
                }
                if (!string.IsNullOrWhiteSpace(strArea))
                {
                    var lArea = JsonConvert.DeserializeObject<List<Model.Area>>(strArea);

                    #region 数据入库
                    using (var conn = new SqlConnection(connection))
                    {
                        conn.Open();
                        conn.Execute("truncate table Area;");
                        IDbTransaction transaction = conn.BeginTransaction();
                        try
                        {
                            lArea.ForEach(area =>
                            {
                                conn.Execute(" insert into Area(Id,ParentId,CityName) values (@Id,@ParentId,@CityName)", area, transaction);
                            });
                            transaction.Commit();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Log.errorlog(ex);
                        }
                        conn.Close();
                    }
                    #endregion
                }
            }
            return false;
        }
        #endregion

        #region 学校年级关系表导入
        /// <summary>
        /// 学校年级关系表导入
        /// </summary>
        /// <returns></returns>
        public bool GetDicGradeInfo()
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                string strDgi = string.Empty;
                try
                {
                    strDgi = wc.DownloadString(ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetDicGradeInfo");
                }
                finally
                {
                    wc.Dispose();
                }
                if (!string.IsNullOrWhiteSpace(strDgi))
                {
                    var lgi = JsonConvert.DeserializeObject<List<Model.GradeInfo>>(strDgi);

                    #region 数据入库
                    using (var conn = new SqlConnection(connection))
                    {
                        conn.Open();
                        conn.Execute("truncate table GradeInfo;");
                        IDbTransaction transaction = conn.BeginTransaction();
                        try
                        {
                            lgi.ForEach(gi =>
                            {
                                conn.Execute("insert into GradeInfo(Id,SchoolId,GradeId,CreateTime,UpdateTime) values (@Id,@SchoolId,@GradeId,@CreateTime,@UpdateTime)", gi, transaction);
                            });
                            transaction.Commit();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Log.errorlog(ex);
                        }
                        conn.Close();
                    }
                    #endregion
                }
            }
            return false;
        }
        #endregion

        #region 教学时间表导入
        /// <summary>
        /// 教学时间表导入
        /// </summary>
        /// <returns></returns>
        public bool GetDicTeachTime()
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                string strDtt = string.Empty;
                try
                {
                    strDtt = wc.DownloadString(ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetDicTeachTime");
                }
                finally
                {
                    wc.Dispose();
                }
                if (!string.IsNullOrWhiteSpace(strDtt))
                {
                    var ldtt = JsonConvert.DeserializeObject<List<Model.DicTeachTime>>(strDtt);

                    #region 数据入库
                    using (var conn = new SqlConnection(connection))
                    {
                        conn.Open();
                        conn.Execute("truncate table DicTeachTime;");
                        IDbTransaction transaction = conn.BeginTransaction();
                        try
                        {
                            ldtt.ForEach(dtt =>
                            {
                                conn.Execute(" insert into DicTeachTime(Id,StypeCode,SectionCode,SectionName,StartTime,EndTime,Wbuilding) values (@Id,@StypeCode,@SectionCode,@SectionName,@StartTime,@EndTime,@Wbuilding)", dtt, transaction);
                            });
                            transaction.Commit();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Log.errorlog(ex);
                        }
                        conn.Close();
                    }
                    #endregion
                }
            }
            return false;
        }
        #endregion

        #region 学科表导入
        /// <summary>
        /// 学科表导入
        /// </summary>
        /// <returns></returns>
        public bool GetDicSubject()
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                string strDs = string.Empty;
                try
                {
                    strDs = wc.DownloadString(ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetDicSubject");
                }
                finally
                {
                    wc.Dispose();
                }
                if (!string.IsNullOrWhiteSpace(strDs))
                {
                    var lds = JsonConvert.DeserializeObject<List<Model.DicSubject>>(strDs);

                    #region 数据入库
                    using (var conn = new SqlConnection(connection))
                    {
                        conn.Open();
                        conn.Execute("truncate table DicSubject;");
                        IDbTransaction transaction = conn.BeginTransaction();
                        try
                        {
                            lds.ForEach(ds =>
                            {
                                conn.Execute(" insert into DicSubject(StypeCode,SubjectCode,SubjectName) values (@StypeCode,@SubjectCode,@SubjectName) ", ds, transaction);
                            });
                            transaction.Commit();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Log.errorlog(ex);
                        }
                        conn.Close();
                    }
                    #endregion
                }
            }
            return false;
        }
        #endregion

        #region 试题类型表导入
        /// <summary>
        /// 试题类型表导入
        /// </summary>
        /// <returns></returns>
        public bool GetDicQuestionType()
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                string strDs = string.Empty;
                try
                {
                    strDs = wc.DownloadString(ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetDicQuestionType");
                }
                finally
                {
                    wc.Dispose();
                }
                if (!string.IsNullOrWhiteSpace(strDs))
                {
                    var ldqt = JsonConvert.DeserializeObject<List<Model.DicQuestionType>>(strDs);

                    #region 数据入库
                    using (var conn = new SqlConnection(connection))
                    {
                        conn.Open();
                        conn.Execute("truncate table DicQuestionType;");
                        IDbTransaction transaction = conn.BeginTransaction();
                        try
                        {
                            ldqt.ForEach(dqt =>
                            {
                                conn.Execute(" insert into DicQuestionType(ID,FatherCode,QtypeCode,QtypeName,StypeCode,SubjectCode,BasicFather,IsEable) values (@ID,@FatherCode,@QtypeCode,@QtypeName,@StypeCode,@SubjectCode,@BasicFather,@IsEable)", dqt, transaction);
                            });
                            transaction.Commit();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Log.errorlog(ex);
                        }
                        conn.Close();
                    }
                    #endregion
                }
            }
            return false;
        }
        #endregion

        #region 资源类型表导入
        /// <summary>
        /// 资源类型表导入
        /// </summary>
        /// <returns></returns>
        public bool GetDicResourceType()
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                string strDrt = string.Empty;
                try
                {
                    strDrt = wc.DownloadString(ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetDicResourceType");
                }
                finally
                {
                    wc.Dispose();
                }
                if (!string.IsNullOrWhiteSpace(strDrt))
                {
                    var ldrt = JsonConvert.DeserializeObject<List<Model.DicResourceType>>(strDrt);

                    #region 数据入库
                    using (var conn = new SqlConnection(connection))
                    {
                        conn.Open();
                        conn.Execute("truncate table DicResourceType;");
                        IDbTransaction transaction = conn.BeginTransaction();
                        try
                        {
                            ldrt.ForEach(drt =>
                            {
                                conn.Execute(" insert into DicResourceType(ReTypeCode,ReTypeName,IsEable) values (@ReTypeCode,@ReTypeName,@IsEable)", drt, transaction);
                            });
                            transaction.Commit();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Log.errorlog(ex);
                        }
                        conn.Close();
                    }
                    #endregion
                }
            }
            return false;
        }
        #endregion

        #region 教师教学行为表导入
        /// <summary>
        /// 教师教学行为表导入
        /// </summary>
        /// <returns></returns>
        public bool GetDicTeacherAction()
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                string strDta = string.Empty;
                try
                {
                    strDta = wc.DownloadString(ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetDicTeacherAction");
                }
                finally
                {
                    wc.Dispose();
                }
                if (!string.IsNullOrWhiteSpace(strDta))
                {
                    var ldta = JsonConvert.DeserializeObject<List<Model.DicTeacherAction>>(strDta);

                    #region 数据入库
                    using (var conn = new SqlConnection(connection))
                    {
                        conn.Open();
                        conn.Execute("truncate table DicTeacherAction;");
                        IDbTransaction transaction = conn.BeginTransaction();
                        try
                        {
                            ldta.ForEach(dta =>
                            {
                                conn.Execute(" insert into DicTeacherAction(TactionCode,TactionName,TactionType) values (@TactionCode,@TactionName,@TactionType)", dta, transaction);
                            });
                            transaction.Commit();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Log.errorlog(ex);
                        }
                        conn.Close();
                    }
                    #endregion
                }
            }
            return false;
        }
        #endregion

        #region 学生学习行为表导入
        /// <summary>
        /// 学生学习行为表导入
        /// </summary>
        /// <returns></returns>
        public bool GetDicStudentAction()
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                string strDsa = string.Empty;
                try
                {
                    strDsa = wc.DownloadString(ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetDicStudentAction");
                }
                finally
                {
                    wc.Dispose();
                }
                if (!string.IsNullOrWhiteSpace(strDsa))
                {
                    var ldsa = JsonConvert.DeserializeObject<List<Model.DicStudentAction>>(strDsa);

                    #region 数据入库
                    using (var conn = new SqlConnection(connection))
                    {
                        conn.Open();
                        conn.Execute("truncate table DicStudentAction;");
                        IDbTransaction transaction = conn.BeginTransaction();
                        try
                        {
                            ldsa.ForEach(dsa =>
                            {
                                conn.Execute("insert into DicStudentAction(SactionCode,SactionName,SactionType) values (@SactionCode,@SactionName,@SactionType);", dsa, transaction);
                            });
                            transaction.Commit();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Log.errorlog(ex);
                        }
                        conn.Close();
                    }
                    #endregion
                }
            }
            return false;
        }
        #endregion

        #region 菜单表导入
        /// <summary>
        /// 菜单表导入
        /// </summary>
        /// <returns></returns>
        public bool GetSysMenuInfo()
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                string strDsa = string.Empty;
                try
                {
                    strDsa = wc.DownloadString(ConfigHelper.GetConfigString("RemoteUrl") + "/Ayyfford/GetSysMenuInfo");
                }
                finally
                {
                    wc.Dispose();
                }
                if (!string.IsNullOrWhiteSpace(strDsa))
                {
                    var lsmi = JsonConvert.DeserializeObject<List<Model.SysMenuInfo>>(strDsa);

                    #region 数据入库
                    using (var conn = new SqlConnection(connection))
                    {
                        conn.Open();
                        conn.Execute("truncate table SysMenuInfo;");
                        IDbTransaction transaction = conn.BeginTransaction();
                        try
                        {
                            lsmi.ForEach(smi =>
                            {
                                conn.Execute("insert into SysMenuInfo(ID,MenuName,MenuType,MenuUrl,MenuLevel,FatherMenuID,Sort,IsNewTab,Icon) values (@ID,@MenuName,@MenuType,@MenuUrl,@MenuLevel,@FatherMenuID,@Sort,@IsNewTab,@Icon);", smi, transaction);
                            });
                            transaction.Commit();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Log.errorlog(ex);
                        }
                        conn.Close();
                    }
                    #endregion
                }
            }
            return false;
        }
        #endregion
    }
}
